/**
 * 
 */
package edu.umd.clip.lm.programs;

import java.io.File;
import edu.berkeley.nlp.util.*;

import edu.umd.clip.lm.model.*;
import edu.umd.clip.lm.model.training.*;
/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class Interpolate {
	public static class Options {
        @Option(name = "-config", required = true, usage = "XML config file")
		public String config;
        @Option(name = "-jobs", required = false, usage = "number of concurrent jobs (default: 1)")
        public int jobs = 1;
        @Option(name = "-forest", required = true, usage = "the decision tree forest")
		public String forest;        
        @Option(name = "-datadir", required = false, usage = "the directory for the temporary files")
		public String datadir = null;        
        @Option(name = "-jerboa", required = false, usage = "use Jerboa storage (default: false)")
		public boolean useJerboa = false;        
        @Option(name = "-compact", required = false, usage = "use Compact storage (default: false)")
		public boolean useCompact = false;        
        @Option(name = "-bdb", required = false, usage = "use Berkeley DB storage (default: false)")
		public boolean useBDB = false;        
        
        @Option(name = "-set-const", required = false, usage = "set all weights to 0.5 (no optimization) (default: false)")
		public boolean setConst = false;        

        @Option(name = "-skip-data", required = false, usage = "skip data population step (must be computed in a previous run) (default: false)")
		public boolean skipData = false;        
        
        @Option(name = "-reuse-weights", required = false, usage = "use existing weights for initialization (default: false)")
		public boolean reuseWeights = false;        

        @Option(name = "-runs", required = false, usage = "number of random initializations (default: 1)")
		public int nrRuns = 1;        
	}
	
	public static void main(String[] args) throws Exception {
        OptionParser optParser = new OptionParser(Options.class);
        final Options opts = (Options) optParser.parse(args, true);

        LMDecodingOptions lmOpts = new LMDecodingOptions();
        lmOpts.config = opts.config;
        lmOpts.jobs = opts.jobs;
        if (opts.forest != null) lmOpts.forest = opts.forest;
        
        if (opts.useJerboa) lmOpts.storage = LMDecodingOptions.Storage.JERBOA;
        if (opts.useBDB) lmOpts.storage = LMDecodingOptions.Storage.BDB;
        if (opts.useCompact) lmOpts.storage = LMDecodingOptions.Storage.COMPACT;

        lmOpts.dontOpenCurrentLMStorage = true;
        
        LanguageModel.initDecoding(lmOpts);

        Experiment experiment = Experiment.getInstance();
		
		ForestModel forest  = experiment.getForest(opts.forest);

		File tmpDir = opts.datadir == null ? new File("populate-" + forest.getName()) : new File(opts.datadir);
		if (!tmpDir.isDirectory()) {
			tmpDir.mkdirs();
		}
		
        ForestInterpolation interpolator = new ForestInterpolation(forest);

        if (opts.setConst) {
        	interpolator.setConst();
        } else {
	        interpolator.initialize(tmpDir, opts.skipData);
	        interpolator.interpolate(opts.reuseWeights, opts.nrRuns);
        }
        /*
        for(LanguageModel lm : forest.getModels()) {
        	lm.saveHistoryTree();
        }
        */
		//forest.getDecoder().getStorage().closeAll();
	}
}
